﻿#region Usings

using System.Security.Principal;
using System.Web;
using System.Web.SessionState;
using Kazkommertsbank.Framework.Model;

#endregion

namespace Kazkommertsbank.Framework
{
    public abstract class KkbResponseHandlerBase : IHttpHandler
    {
        private HttpContext _context;

        public HttpContext Context
        {
            get { return _context; }
        }

        public HttpApplication ApplicationInstance
        {
            get { return Context.ApplicationInstance; }
        }

        public HttpApplicationState Application
        {
            get { return Context.Application; }
        }

        public HttpServerUtility Server
        {
            get { return Context.Server; }
        }

        public HttpSessionState Session
        {
            get { return Context.Session; }
        }

        public HttpRequest Request
        {
            get { return Context.Request; }
        }

        public HttpResponse Response
        {
            get { return Context.Response; }
        }

        public IPrincipal User
        {
            get { return Context.User; }
        }

        #region Implementation of IHttpHandler

        /// <summary>
        ///   Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see
        ///    cref="T:System.Web.IHttpHandler" /> interface.
        /// </summary>
        /// <param name="context"> An <see cref="T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests. </param>
        public void ProcessRequest(HttpContext context)
        {
            _context = context;

            try
            {
                ProcessRequest();
            }
            finally
            {
                _context = null;
            }
        }

        /// <summary>
        ///   Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler" /> instance.
        /// </summary>
        /// <returns> true if the <see cref="T:System.Web.IHttpHandler" /> instance is reusable; otherwise, false. </returns>
        public virtual bool IsReusable
        {
            get { return false; }
        }

        #endregion

        public virtual void ProcessRequest()
        {
            KkbResponseParameters responseParamaters = new KkbResponseParameters(Request.Form);
            ResponseDocumentBase paymentResponse = responseParamaters.GetKazkommertsbankResponse();
            ProcessKkbPaymentResponse(paymentResponse);
            Response.Write("0");
            Response.End();
        }

        protected abstract void ProcessKkbPaymentResponse(ResponseDocumentBase paymentResponse);
    }
}